package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询该数据是否存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //存在 number++

        if (shoppingCartList.size()>0&&shoppingCartList!=null) {
            //获取list中的第一个元素，并设置好对象shoppingCart;
            shoppingCart.setId(shoppingCartList.get(0).getId());
            shoppingCart.setNumber(shoppingCartList.get(0).getNumber()+1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //不存在  判断当前是套餐还是菜品
            if(shoppingCartDTO.getDishId()!=null){
                //查询dish数据
                Dish dish=dishMapper.getById(shoppingCartDTO.getDishId());
                //设置菜品名称
                shoppingCart.setName(dish.getName());
                //设置菜品id
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                //设置图片
                shoppingCart.setImage(dish.getImage());
                //设置口味
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                //设置金钱
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //查询setmeal数据
                Setmeal setmeal=setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                //设置套餐id
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                //设置图片
                shoppingCart.setImage(setmeal.getImage());
                //设置金钱
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //设置数量
            shoppingCart.setNumber(1);
            //设置时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入数据
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart(Long userId) {
        ShoppingCart shoppingCart=ShoppingCart.builder().userId(userId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean(Long userId) {
        shoppingCartMapper.delete(userId);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询该数据是否存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList.size()>0&&shoppingCartList!=null) {
            shoppingCart=shoppingCartList.get(0);
            shoppingCart.setUserId(BaseContext.getCurrentId());

            if (shoppingCart.getNumber()==1){
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.update(shoppingCart);
            }

        }
    }
}
